package cn.juque.jdfsystem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.juque.common.constants.NumberConstant;
import cn.juque.jdfsystem.bo.StatisticsCountBO;
import cn.juque.jdfsystem.bo.StatisticsFileBO;
import cn.juque.jdfsystem.cache.TpsCache;
import cn.juque.jdfsystem.domain.StatisticsFileCountDO;
import cn.juque.jdfsystem.entity.FileInfo;
import cn.juque.jdfsystem.entity.RegisterInfo;
import cn.juque.jdfsystem.enums.RegisterExecuteStatusEnum;
import cn.juque.jdfsystem.mapper.FileInfoMapper;
import cn.juque.jdfsystem.mapper.RegisterInfoMapper;
import cn.juque.jdfsystem.service.IStatisticsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>StatisticsServiceImpl</li>
 * </ul>
 * @date 2023-05-06 11:05:03
 **/
@Service("statisticsService")
public class StatisticsServiceImpl implements IStatisticsService {

    @Resource
    private RegisterInfoMapper registerInfoMapper;

    @Resource
    private FileInfoMapper fileInfoMapper;

    @Resource
    private TpsCache tpsCache;

    /**
     * 统计数量
     *
     * @return StatisticsCountBO
     */
    @Override
    public StatisticsCountBO count() {
        StatisticsCountBO statisticsCountBO = new StatisticsCountBO();
        statisticsCountBO.setOnLineInstance(NumberConstant.ZERO);
        statisticsCountBO.setTotalInstance(NumberConstant.ZERO);
        statisticsCountBO.setUploadTps(0.00);
        statisticsCountBO.setDownloadTps(0.00);
        List<RegisterInfo> registerInfoList = this.registerInfoMapper.queryInfoList(new RegisterInfo());
        if (CollUtil.isNotEmpty(registerInfoList)) {
            long onlineCount = registerInfoList.stream()
                    .filter(f -> RegisterExecuteStatusEnum.NORMAL.getCode().equals(f.getExecuteStatus())).count();
            statisticsCountBO.setOnLineInstance((int) onlineCount);
            statisticsCountBO.setTotalInstance(registerInfoList.size());
        }
        Long fileCount = this.fileInfoMapper.selectCount(new QueryWrapper<>(new FileInfo()));
        statisticsCountBO.setFileCount(fileCount);
        statisticsCountBO.setUploadTps(this.tpsCache.getUploadTps());
        statisticsCountBO.setDownloadTps(this.tpsCache.getDownloadTps());
        return statisticsCountBO;
    }

    /**
     * 统计文件数量
     *
     * @param days 统计的天数
     * @return StatisticsFileBO
     */
    @Override
    public StatisticsFileBO fileCount(Integer days) {
        StatisticsFileBO statisticsFileBO = new StatisticsFileBO();
        Date beforeDate = DateUtil.offsetDay(new Date(), -days);
        String start = DateUtil.format(beforeDate, DatePattern.PURE_DATE_PATTERN);
        String end = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
        List<StatisticsFileCountDO> fileCountList = this.fileInfoMapper.statisticsFileCount(start, end);
        List<String> dayList = fileCountList.stream().map(StatisticsFileCountDO::getCreateDate).collect(Collectors.toList());
        statisticsFileBO.setDayList(dayList);
        List<Integer> fileCounts = fileCountList.stream().map(StatisticsFileCountDO::getCount).collect(Collectors.toList());
        statisticsFileBO.setFileCountList(fileCounts);
        return statisticsFileBO;
    }
}
